package com.android.dwt.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.android.dwt.R;
import com.android.dwt.data.GTDApp;
import com.android.dwt.fragment.ContextFrag;
import com.android.dwt.fragment.ListFrag;
import com.android.dwt.fragment.LocationFrag;
import com.android.dwt.fragment.MoreFrag;
import com.android.dwt.fragment.TodayFrag;
import com.android.dwt.util.C;

public class MainAct extends FragmentActivity
		implements OnPageChangeListener, OnTouchListener {
	
	private class MyPagerAdapter extends FragmentPagerAdapter {
        
        public MyPagerAdapter(FragmentManager fm) {
        	
			super(fm);
		}
        
        @Override
        public Object instantiateItem(View container, int position) {
        	
        	Log.e(TAG, "instantiateItem is called! position= " + position);
        	
        	return super.instantiateItem(container, position);
        }
        
        @Override
        public int getItemPosition(Object object) {
        	
        	Log.e(TAG, "getItemPosition is called!");
        	
        	return super.getItemPosition(object);
        }
        
		@Override
        public int getCount() {
			
//			Log.e(TAG, "getCount is called! count=" + mFrags.size());
			
//            return mListViews.size();
			
			return mFrags.size();
        }

		@Override
		public Fragment getItem(int arg0) {
			
//			return null;
			
//			return ArrayListFragment.newInstance(arg0);
			
			Log.e(TAG, "get item " + arg0);
			
			return mFrags.get(arg0);
		}
    
	}		// end inner class

	
	private static final String TAG = "MainAct";
	public static final int LIST = 0;
	public static final int TODAY = 1;
	public static final int CONTEXT = 2;
	public static final int LOC = 3;
	public static final int MORE = 4;
	
//	private MainFrag mFrag;
	private GTDApp mGa;
	private Resources mRsrc;
	
	private ViewPager mViewPager;
	private MyPagerAdapter mAdapter;
	private ArrayList<Fragment> mFrags;
	
	private Calendar mCal = Calendar.getInstance();
	private SimpleDateFormat mSdfDate = new SimpleDateFormat("yyyy-MM-dd");
	
//	private RelativeLayout mList;
//	private RelativeLayout mToday;
//	private RelativeLayout mContext;
//	private RelativeLayout mLoc;
//	private RelativeLayout mMore;
	
	private TextView mIndList;
	private TextView mIndToday;
	private TextView mIndContext;
	private TextView mIndLoc;
	private TextView mIndMore;
	
	private TextView mCurIndicator;
	private TextView mTodayCount;
	

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.e(TAG, "onCreate is called!");
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        mGa = (GTDApp) getApplication();
        mRsrc = getResources();
        
       /* mFrag = MainFrag.newInstance();
        getSupportFragmentManager().beginTransaction()
        	.add(R.id.frag_content, mFrag)
        	.commit();*/
        
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        
        mViewPager = (ViewPager) findViewById(R.id.vp_content);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(this);
		
		initFrags();
        
        mViewPager.setCurrentItem(LIST);
        
        initBottom();
    }
    
    public void updateCounter(int num) {
    	
//    	Cursor csr = mGa.getDbta().getDayTask(mSdfDate.format(mCal.getTime()));
    	if (num <= 0) {
    		
    		mTodayCount.setVisibility(View.INVISIBLE);
    		
    	} else {
    		
    		mTodayCount.setVisibility(View.VISIBLE);
    		mTodayCount.setText(String.valueOf(num));
    	}
    	
//    	csr.close();
    }
    
    private void initBottom() {
    	
//    	mList = (RelativeLayout) findViewById(R.id.rl_list);
//    	mList.setOnClickListener(this);
    	((TextView) findViewById(R.id.tv_list)).setOnTouchListener(this);
    	mIndList = (TextView) findViewById(R.id.tv_list_indicator);
    	
//    	mToday = (RelativeLayout) findViewById(R.id.rl_today);
//    	mToday.setOnClickListener(this);
    	((TextView) findViewById(R.id.tv_today)).setOnTouchListener(this);
    	mIndToday = (TextView) findViewById(R.id.tv_today_indicator);
    	mTodayCount = (TextView) findViewById(R.id.tv_today_count);
    	
//    	mContext = (RelativeLayout) findViewById(R.id.rl_context);
//    	mContext.setOnClickListener(this);
    	((TextView) findViewById(R.id.tv_context)).setOnTouchListener(this);
    	mIndContext = (TextView) findViewById(R.id.tv_context_indicator);
    	
//    	mLoc = (RelativeLayout) findViewById(R.id.rl_loc);
//    	mLoc.setOnClickListener(this);
    	((TextView) findViewById(R.id.tv_loc)).setOnTouchListener(this);
    	mIndLoc = (TextView) findViewById(R.id.tv_loc_indicator);
    	
//    	mMore = (RelativeLayout) findViewById(R.id.rl_more);
//    	mMore.setOnClickListener(this);
    	((TextView) findViewById(R.id.tv_more)).setOnTouchListener(this);
    	mIndMore = (TextView) findViewById(R.id.tv_more_indicator);
    	
    	mCurIndicator = mIndList;
    	mCurIndicator.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    }
    
    private void initFrags() {
    	
    	mFrags = new ArrayList<Fragment>();
		
    	// list
		mFrags.add(ListFrag.newInstance());
		
		// today
		mFrags.add(TodayFrag.newInstance());
		
		// context
		mFrags.add(ContextFrag.newInstance());
		
		// location
		mFrags.add(LocationFrag.newInstance());
		
		// more
		mFrags.add(MoreFrag.newInstance());
    }
    
    @Override
    public void overridePendingTransition(int enterAnim, int exitAnim) {
    	
    	super.overridePendingTransition(enterAnim, exitAnim);
    }
    
    public ArrayList<Fragment> getFragments() {
    	
    	return mFrags;
    }
    
    @Override
    protected void onStart() {
    	
    	Log.e(TAG, "onStart is called!");
    	
    	super.onStart();
    }
    
    @Override
    protected void onResume() {
    	
    	Log.e(TAG, "onResume is called!");
    	
    	super.onResume();
    	
//    	mAdapter.notifyDataSetChanged();
    }
    
    @Override
    protected void onPause() {
    	
    	Log.e(TAG, "onPause is called!");
    	
    	super.onPause();
    }
    
    @Override
    protected void onStop() {
    	
    	Log.e(TAG, "onStop is called!");
    	
    	super.onStop();
    }

	@Override
	public void onPageScrollStateChanged(int arg0) {
		
		Log.e(TAG, "onPageScrollStateChanged is triggered! " + arg0);
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		
		Log.e(TAG, "onPageSelected: " + arg0);
		
		/*if(arg0 == 0) {
        	
        	mViewPager.setCurrentItem(mFrags.size()-2);
        	
        } else if (arg0 == (mFrags.size()-1)) {
        	
        	mViewPager.setCurrentItem(1);
        }*/
		
		switch(arg0) {
		
    		case LIST:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndList.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndList;
    			
    			break;
    			
    		case TODAY:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndToday.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mIndList.startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
    			
    			mCurIndicator = mIndToday;
    			
    			break;
    			
    		case CONTEXT:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndContext.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndContext;
    			
    			break;
    			
    		case LOC:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndLoc.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndLoc;
    			
    			break;
    			
    		case MORE:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndMore.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndMore;
    			
    			break;
    			
    		default:
    			break;
		}
        
        return;
	}

	/*@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
    		case R.id.rl_list:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndList.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndList;
    			
    			mViewPager.setCurrentItem(LIST);
    			
    			break;
    			
    		case R.id.rl_today:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndToday.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndToday;
    			
    			mViewPager.setCurrentItem(TODAY);
    			
    			break;
    			
    		case R.id.rl_context:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndContext.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndContext;
    			
    			mViewPager.setCurrentItem(CONTEXT);
    			
    			break;
    			
    		case R.id.rl_loc:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndLoc.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndLoc;
    			
    			mViewPager.setCurrentItem(LOC);
    			
    			break;
    			
    		case R.id.rl_more:
    			
    			mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    			mIndMore.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    			
    			mCurIndicator = mIndMore;
    			
    			mViewPager.setCurrentItem(MORE);
    			
    			break;
    			
    		default:
    			break;
		}
		
		return;
	}*/

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
//		TextView tv = (TextView) v;
		
		if (event.getAction() == MotionEvent.ACTION_UP) {
			
			Log.e(TAG, v.getId() + " up event is triggered!");
			
			switch (v.getId()) {
			
    			case R.id.tv_list:
    				
    				mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
    				mIndList.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
    				
    				mCurIndicator = mIndList;
    				
    				mViewPager.setCurrentItem(LIST);
    				
    				break;
    				
    			case R.id.tv_today:
    				
    				mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
        			mIndToday.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
        			
        			mCurIndicator = mIndToday;
        			
        			mViewPager.setCurrentItem(TODAY);
        			
    				break;
    				
    			case R.id.tv_context:
    				
    				mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
        			mIndContext.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
        			
        			mCurIndicator = mIndContext;
        			
        			mViewPager.setCurrentItem(CONTEXT);
        			
    				break;
    				
    			case R.id.tv_loc:
    				
    				mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
        			mIndLoc.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
        			
        			mCurIndicator = mIndLoc;
        			
        			mViewPager.setCurrentItem(LOC);
        			
    				break;
    				
    			case R.id.tv_more:
    				
    				mCurIndicator.setBackgroundColor(Color.TRANSPARENT);
        			mIndMore.setBackgroundColor(mRsrc.getColor(R.color.bottom_indicator));
        			
        			mCurIndicator = mIndMore;
        			
        			mViewPager.setCurrentItem(MORE);
        			
    				break;
    				
    			default:
    				break;
		
			}
			
			v.setBackgroundColor(Color.TRANSPARENT);
			
			return true;
		}

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			
			v.setBackgroundResource(R.drawable.bg_hl);
			
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
//		return super.onCreateOptionsMenu(menu);
		
		boolean result = super.onCreateOptionsMenu(menu);
		
		menu.add(0, C.MENU.NEW_TASK, Menu.NONE, getString(R.string.menu_new_task))
			.setIcon(android.R.drawable.ic_menu_add);
		
		menu.add(0, C.MENU.SEARCH, Menu.NONE, getString(R.string.menu_search)).setIcon(
				android.R.drawable.ic_menu_search);
		
		menu.add(0, C.MENU.SETTINGS, Menu.NONE, getString(R.string.menu_settings)).setIcon(
				R.drawable.menu_setting);
		
		return result;
	}
   
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		Intent itt = null;
		// int index = myListView.getSelectedItemPosition();
		
		switch (item.getItemId()) {
		
			case C.MENU.NEW_TASK:
				
				itt = new Intent(MainAct.this, NewTaskAct.class);
				
				itt.putExtra("list_index", 0);
				itt.putExtra("new_task", true);
				
//				startActivityForResult(itt, 0);
				startActivity(itt);
				
				return true;
				
			case C.MENU.SEARCH:
				
				onSearchRequested();
				
//				itt = getIntent();
//				itt.setClass(MainAct.this, SearchAct.class);
//				itt = new Intent(MainAct.this, SearchAct.class);
//				
//				startActivity(itt);
				return true;
				
			case C.MENU.SETTINGS:
				
				itt = new Intent(MainAct.this, SettingAct.class);
    			startActivity(itt);
	
			default:
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}
    
}
